package com.hit.eduService.controller;


import com.hit.commonUtils.R;
import com.hit.eduService.client.VodClient;
import com.hit.eduService.entity.EduVideo;
import com.hit.eduService.service.EduVideoService;
import com.hit.serviceBase.exceptionHandler.MyException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;


/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-11-04
 */
@RestController
@CrossOrigin
@RequestMapping("/eduService/edu-video")
public class EduVideoController {
    @Autowired
    private EduVideoService eduVideoService;
    @Qualifier("com.hit.eduService.client.VodClient")
    @Autowired
    private VodClient vodClient;
    //1.添加章节对应的小节
    @PostMapping("addVideo")
    public R addVideo(@RequestBody EduVideo eduVideo){
        eduVideoService.save(eduVideo);
        return R.ok();
    }

    //2.删除小节， 同时把视频也删掉
    @DeleteMapping("{id}")
    public R deleteVideo(@PathVariable String id){
        //删除阿里云处的视频：把VodClient注入EduVideoController中
        //根据小节id获取视频id
        EduVideo eduVideo=eduVideoService.getById(id);
        String videoId=eduVideo.getVideoSourceId();
        if(!StringUtils.isEmpty(videoId)){//videoId存在时才删除
            R result=vodClient.removeVideo(videoId);//传入的是视频id，而不是小节id
            if(result.getCode()==20001){//
                throw new MyException(20001,"删除视频失败，熔断器生效...");
            }
        }
        //删除小节
        eduVideoService.removeById(id);
        return R.ok();
    }

    //3.修改小节
    @PostMapping("updateVideo")
    public R updateVideo(@RequestBody EduVideo eduVideo){
        eduVideoService.updateById(eduVideo);
        return R.ok();
    }
    //4.根据id获取小节的信息
    @GetMapping("getVideoInfo/{id}")
    public R getVideoInfo(@PathVariable String id){
        EduVideo eduVideo=eduVideoService.getById(id);
        return R.ok().data("video",eduVideo);
    }

}

